package com.zt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.mapper.CouponUMapper;
import com.zt.mapper.DetailsMapper;
import com.zt.mapper.OrderMapper;
import com.zt.mapper.ProductMapper;
import com.zt.pojo.CouponU;
import com.zt.pojo.Details;
import com.zt.pojo.Orders;
import com.zt.pojo.Product;
import com.zt.pojo.dto.DetailsDto;
import com.zt.pojo.dto.OrderDto;
import com.zt.pojo.dto.ProductDto;
import com.zt.service.OrderService;
import com.zt.service.ProductService;
import com.zt.util.ResultVo;
import com.zt.util.SnowflakeUtil;
import com.zt.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Date 2025/5/9 19:58
 * @Author neko
 **/
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    DetailsMapper detailsMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CouponUMapper couponUMapper;

    @Override
    public ResultVo addPro(ProductDto dto) {
        String num = "SP"+new Date().getTime()+"JY";
        Product product = new Product();
        product.setId(String.valueOf(SnowflakeUtil.nextId()));
        product.setProNumber(num);
        product.setName(dto.getName());
        product.setPrice(dto.getPrice());
        product.setProductNum(dto.getProductNum());
        product.setCreateTime(new Date());
        productMapper.insert(product);

        return ResultVo.success(200,"添加成功",null);
    }

    @Override
    public ResultVo addOrder(OrderDto dto) {
        String odi = "JY"+new Date().getTime()+"XZC";

        Orders o = new Orders();
        o.setId(String.valueOf(SnowflakeUtil.nextId()));
        o.setOrderId(odi);
        //从token中获取用户id
        String to = (String) redisTemplate.opsForValue().get("token");
        String userid = String.valueOf(TokenUtils.parseJWT(to).getId());
        System.err.println(userid);
        o.setUserId(userid);
        o.setDxNum(0);
        o.setSumPrice(BigDecimal.valueOf(0.0));
        o.setDiscountPrice(BigDecimal.valueOf(0.0));
        o.setPlyStatus("WX"+new Date().getTime()+"ZF");
        o.setCreateTime(new Date());
        orderMapper.insert(o);

        List<BigDecimal> list = new ArrayList<>();
        for(DetailsDto detailsDto:dto.getDtoList()){
            Details details = new Details();
            details.setId(String.valueOf(SnowflakeUtil.nextId()));
            details.setOrderId(o.getOrderId());
            details.setProductBian(detailsDto.getProductBian());
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Product::getProNumber,detailsDto.getProductBian());
            Product p = productMapper.selectOne(queryWrapper);
            details.setOnePrice(p.getPrice());
            BigDecimal xiao = p.getPrice().multiply(BigDecimal.valueOf(detailsDto.getProductNum()));
            details.setSubtotalPrice(xiao);
            details.setProductNum(detailsDto.getProductNum());
            details.setCreateTime(new Date());
            detailsMapper.insert(details);
            list.add(xiao);
        }

        o.setDxNum(list.size());
        BigDecimal z = list.stream().reduce(BigDecimal.ZERO,BigDecimal::add);
        o.setSumPrice(z);
        QueryWrapper<CouponU> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CouponU::getUserId,userid);
        CouponU cu = couponUMapper.selectOne(queryWrapper);
        BigDecimal you = cu.getFaceValue();
        o.setDiscountPrice(you);
        o.setYouJin(z.subtract(you));
        o.setCouponType(cu.getId());
        orderMapper.updateById(o);

        return ResultVo.success(200,"成功",null);
    }
}
